import { _decorator, Component, Node, Graphics, Label, Vec3, Color, Event, instantiate, Vec2, EventTouch, UITransform, Toggle, math } from 'cc';
const { ccclass, property } = _decorator;

import PolygonNav2D from "../../PolygonNav/Script/PolygonNav2D";
import PolygonNavPolygon from '../../PolygonNav/Script/PolygonNavPolygon';
import { EnEvent } from '../../script/Common/const/EnEvent';
import { GameAPI } from '../../script/Common/GameAPI';
import SelfPlayer from './SelfPlayer';

@ccclass('MapMgr')
export default class MapMgr extends Component {

    @property(PolygonNav2D)
    public PolygonNav2D: PolygonNav2D = null;
    @property(SelfPlayer)
    SelfControl: SelfPlayer = null;
    @property(Node)
    PlayerNode: Node = null;
    @property(Graphics)
    SelfGraphics: Graphics = null;
    @property(PolygonNavPolygon)
    Polygon: PolygonNavPolygon[] = [];
    @property(Node)
    createParent: Node = null;

    private _players: SelfPlayer[] = [];
    private _robots: SelfPlayer[] = [];
    private _addIds: number[] = [];

    start() {
        this.SelfControl.Init(this.PolygonNav2D, this.SelfGraphics);
        GameAPI.RegGEvent(EnEvent.AddPolygonEvent, this.OnAddPolygonEvent.bind(this), this);
        GameAPI.RegGEvent(EnEvent.RemovePolygonEvent,this.OnRemovePolygonEvent.bind(this), this);
    }

    onDestroy() {
        GameAPI.OffAllCaller(this);
    }

    OnAddPolygonEvent(arg) {
        for (let index = 0; index < this.Polygon.length; index++) {
            const element = this.Polygon[index];
            if (element.node.name == arg.name) {
                let polygon = element.node.getComponent(PolygonNavPolygon);
                let id = this.PolygonNav2D.AddPolygon(polygon);
                this._addIds.push(id);
                this.SelfControl.AdjustPos();
                for (let index = 0; index < this._robots.length; index++) {
                    this._robots[index].AdjustPos();
                }
                // element.node.active = true;
                return;
            }
        }
    }

    OnRemovePolygonEvent(arg)
    {
        let index = this.PolygonNav2D.GetPolygonNav2DByName(arg.obstacle);
        this.PolygonNav2D.RemovePolygon(index);
    }

    private AddPolygon() {
        let pos = new Vec3(Math.random() * 1600 - 800, Math.random() * 1600 - 800);
        pos = this.SelfControl.node.position
        let index = Math.floor(Math.random() * this.Polygon.length);
        index = 0
        let node = instantiate(this.Polygon[index].node);
        node.parent = this.node;
        node.setPosition(pos);
        let polygon = node.getComponent(PolygonNavPolygon);
        let id = this.PolygonNav2D.AddPolygon(polygon);
        this._addIds.push(id);

        this.SelfControl.AdjustPos();
        for (let index = 0; index < this._robots.length; index++) {
            this._robots[index].AdjustPos();
        }
    }

    private RemovePolygon() {
        if (this._addIds.length > 0) {
            let id = this._addIds.pop();
            this.PolygonNav2D.RemovePolygon(id);
        }
        let index = this.PolygonNav2D.GetPolygonNav2DByName("Obstacle10");
        this.PolygonNav2D.RemovePolygon(index);
    }

    private AddRobot() {
        let robot = this.CreatePlayer();
        robot.Init(this.PolygonNav2D, null, true);
        this._robots.push(robot);
    }

    private MinusRobot() {
        if (this._robots.length > 0) {
            let robot = this._robots.pop();
            robot.node.parent = this.PlayerNode.parent;
            robot.Graphics.clear();
        }
    }

    private CreatePlayer(): SelfPlayer {
        if (this._players.length > 0) {
            return this._players.pop();
        }
        else {
            let playerNode = instantiate(this.PlayerNode);
            playerNode.parent = this.node;
            let pos = new Vec2(Math.random() * 2048 - 1024, Math.random() * 2048 - 1024);
            if (!this.PolygonNav2D.PointIsValid(pos)) {
                pos = this.PolygonNav2D.GetCloserEdgePoint(pos);
            }
            playerNode.setPosition(new Vec3(pos.x, pos.y));
            return playerNode.getComponent(SelfPlayer);
        }
    }

}


